class ListNode {  
  constructor(val = 0, next = null) {  
      this.val = val;  
      this.next = next;  
  }  
}  

// Function to convert an array to a linked list  
function arrayToList(arr) {  
  let dummyHead = new ListNode();  
  let current = dummyHead;  
  for (let value of arr) {  
      current.next = new ListNode(value);  
      current = current.next;  
  }  
  return dummyHead.next; // return the real head  
}  

var mergeTwoLists = function (list1, list2) {
  let head = new ListNode();
  let cur = head;
  while (list1 && list2) {
    if (list1.val < list2.val) {
      cur.next = list1;
      list1 = list1.next;
    } else {
      cur.next = list2;
      list2 = list2.next;
    }
    cur = cur.next;
  }
  cur.next = list1 ? list1 : list2;
  return head.next;
};

const list1 = [1, 2, 4]
const list2 = [1, 3, 4]
console.log(mergeTwoLists(list1, list2));
